package cn.com.yitong.demo.demo1.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import sqlmap.DemoTable1;
import cn.com.yitong.component.adapter.core.spi.service.AdapterService;
import cn.com.yitong.demo.demo1.dao.DemoTable1Dao;
import cn.com.yitong.framework.core.base.service.BaseServiceImpl;
import cn.com.yitong.framework.core.message.vo.ResponseMessageVO;
import cn.com.yitong.framework.core.util.query.Parameters;
import cn.com.yitong.framework.support.context.AresContext;
import cn.com.yitong.framework.util.JsonHelper;
import cn.com.yitong.framework.util.StrHelper;

/**
 * @ClassName: Demo1ServiceImpl
 * @Description: TODO
 * @author: 孙孟（sm@yitong.com.cn）
 * @date: 2015-9-7 上午9:54:59
 */
public class Demo1ServiceImpl extends BaseServiceImpl<DemoTable1, DemoTable1Dao> implements Demo1Service {

	/**
	 * @FieldName: adapterService
	 * @FieldType: AdapterService
	 * @Description: 注入HTTP适配器SPI（使用注解）
	 * Socket适配器SPI socketAdapterService
	 * JMS适配器SPI jmsAdapterService
	 * WebService适配器SPI soapAdapterService
	 */
	@Autowired
	@Qualifier("httpAdapterService")
	private AdapterService adapterService;
	
	/* 
	 * (non Javadoc)
	 * @Title: query
	 * @Description: 分页查询
	 * @param context 请求上下文
	 * @param query 查询参数
	 * @return ResponseMessageVO
	 * @see cn.com.yitong.demo.demo1.service#query(cn.com.yitong.framework.core.context.AresContext, cn.com.yitong.framework.core.util.query.Parameters)
	 */
	@Override
	public ResponseMessageVO query(AresContext context, Parameters parameters) {
		
		//调用DAO分页查询接口，得到指定页数据
		//分页查询
		//sql key
		String sqlkey = "query";
		ResponseMessageVO response = this.getBaseDao().pagedQuery(sqlkey, parameters);
		//标记需要返回的属性
		response.setMarkProperties("id", "name", "type", "remark", "lastModiDate", "status");
		//this.adapterService.executeRequest(context, "demo1Service/query", parameters);
		return response;
	}

	/* 
	 * (non Javadoc)
	 * @Title: getDataByName
	 * @Description: 根据名称，查询数据
	 * @param context
	 * @param name
	 * @return
	 * @see cn.com.yitong.demo.demo1.service.Demo1Service#getDataByName(cn.com.yitong.framework.support.context.AresContext, java.lang.String)
	 */
	@Override
	public ResponseMessageVO getDataByName(AresContext context, String name) {
		//
		ResponseMessageVO response = new ResponseMessageVO();
		
		context.getRequestParameter("name");
		
		//自定义的HQL查询
		List<DemoTable1> list = this.getBaseDao().queryDataByName(name);
		//标记需要跳过的属性
		response.setIgnoreProperties("children");

		response.setResult(list);
		return response;
	}

	/* 
	 * (non Javadoc)
	 * @Title: getDataByType
	 * @Description: 根据类型，查询数据
	 * @param context
	 * @param typeId
	 * @return
	 * @see cn.com.yitong.demo.demo1.service.Demo1Service#getDataByType(cn.com.yitong.framework.support.context.AresContext, java.lang.String)
	 */
	@Override
	public ResponseMessageVO getDataByType(AresContext context, String typeId) {
		ResponseMessageVO response = context.getResponseMessage();
		//自定义的HQL查询
		List<DemoTable1> list = this.getBaseDao().queryDataByType(typeId);

		response.setResult(list);
		return response;
	}
	
	/* 
	 * (non Javadoc)
	 * @Title: insert
     * @Description: 新增
     * @param context 请求上下文
     * @param vo 实体
     * @return ResponseMessageVO（默认返回新增记录的主键）
	 * @see cn.com.yitong.framework.core.base.service.BaseServiceImpl#insert(cn.com.yitong.framework.support.context.AresContext, java.lang.Object)
	 */
	@Override
	public ResponseMessageVO insert(AresContext context, DemoTable1 vo) {
		ResponseMessageVO response = context.getResponseMessage();
		vo.setId(StrHelper.uuid());
		boolean success = this.getBaseDao().insert(vo);
		response.setResult(vo.getId());
		response.setSuccess(success);
		return response;
	}

	/* 
	 * (non Javadoc)
	 * @Title: delete
	 * @Description: 删除
	 * @param context 请求上下文
	 * @param vo
	 * @return
	 * @see cn.com.yitong.framework.core.base.service.BaseServiceImpl#delete(cn.com.yitong.framework.support.context.AresContext, java.lang.String)
	 */
	@Override
	public ResponseMessageVO delete(AresContext context, String id) {
		ResponseMessageVO response = context.getResponseMessage();
		boolean success = this.getBaseDao().delete(id);
		if(!success){
			response.setError("<font color=red>需要删除的数据不存在！</font>");
		}
		response.setSuccess(success);
		return response;
	}

	/* 
	 * (non Javadoc)
	 * @Title: update
	 * @Description: 更新
	 * @param context 请求上下文
	 * @param vo
	 * @return
	 * @see cn.com.yitong.framework.core.base.service.BaseServiceImpl#update(cn.com.yitong.framework.support.context.AresContext, java.lang.Object)
	 */
	@Override
	public ResponseMessageVO update(AresContext context, DemoTable1 vo) {
		ResponseMessageVO response = context.getResponseMessage();
		// 更新
		boolean success = this.getBaseDao().update(vo);
		// 更新记录的ID
		response.setResult(vo.getId());
		response.setSuccess(success);
		return response;
	}

	/* 
	 * (non Javadoc)
	 * @Title: executeUpdate
	 * @Description: 执行数据提交
	 * @param context 请求上下文
	 * @param arg1
	 * @return
	 * @see cn.com.yitong.demo.service.Demo1Service#executeUpdate(cn.com.yitong.framework.support.context.AresContext, java.lang.String)
	 */
	@Override
	public ResponseMessageVO executeUpdate(AresContext context, String arg1) {
		ResponseMessageVO response = context.getResponseMessage();
		boolean success = this.getBaseDao().executeUpdate(arg1);
		response.setSuccess(success);
		return response;
	}
	
//	/**
//	 * 测试
//	 * @param args
//	 * @throws Exception
//	 */
	public static void main(String[] args) throws Exception {
//		String values = "{\"aaa\" : \"123\",\"bbb\" : \"456\"}";
//		Map<String, String> map = JsonHelper.readValue(values, new TypeReference<HashMap<String, String>>(){});
//		System.err.println(JsonHelper.exclude(map));
////		Map map = new HashMap<String, String>();
////		map.put("aaa", "123");
////		map.put("bbb", "456");
////		System.err.println(JsonHelper.exclude(map));
////		Object obj = JsonHelper.readValue(listString, new TypeReference<List<String>>(){}); 
		
		ResponseMessageVO response = new ResponseMessageVO();
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", "value-a");
		map.put("name", "value-b");
		map.put("code", "value-c");
		
		DemoTable1 vo = new DemoTable1();
		vo.setId("value-a");
		vo.setName("value-b");
		vo.setType("value-c");

		response.setResult(map);
////		response.setResult(vo);
//		
////		response.setMarkProperties("id", "name");
		response.setIgnoreProperties("id", "name");
//		
		System.err.println(JsonHelper.formatResult(response));
	}
}
